package com.zb.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zb.entity.Car;
import com.zb.entity.Help;
import com.zb.entity.Store;
import com.zb.entity.User;
import com.zb.entity.Wx;
import com.zb.service.WxService;
import com.zb.serviceimpl.WxServiceImpl;
import com.zb.util.PageUtil;

/**
 * Servlet implementation class WxServlet
 */
public class WxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String op = request.getParameter("op");
		if(op.equals("show")) {
			this.doshow(request, response);
		}else if(op.endsWith("toupdate")) {
			this.toupadte(request, response);
		}else if(op.equals("doupdate")) {
			this.doupadte(request, response);
		}else if(op.equals("del")) {
			this.del(request, response);
		}
		}
		protected void doshow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			WxService w=new WxServiceImpl();
			int index=1;
			String carnum=null;
			if (request.getParameter("index")!=null&&!"".equals(request.getParameter("index"))) {
				index = Integer.parseInt(request.getParameter("index"));
			}
			carnum=request.getParameter("carnum");
				PageUtil<Wx> page = w.getPersonPage(index, carnum);
				request.setAttribute("mypage", page);
				
				
				request.getRequestDispatcher("search_xl.jsp").forward(request, response);
		}
		protected void toupadte(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			WxService w=new WxServiceImpl();
			int id = Integer.parseInt(request.getParameter("id"));
			
			Wx p = w.getYhById(id);
			request.setAttribute("p",p);
			request.getRequestDispatcher("Wxupdate.jsp").forward(request, response);
		}
		protected void doupadte(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int id=Integer.parseInt(request.getParameter("id"));
			String carnum=request.getParameter("carnum");
			String carname=request.getParameter("carname");
			String carair=request.getParameter("carair");
			String serviceExp=request.getParameter("serviceExp");
			String day=request.getParameter("day");
			String serviceMoney=request.getParameter("serviceMoney");
			
			
			String remark=request.getParameter("remark");
			WxService w=new WxServiceImpl();
			Wx ww=new Wx();
			Car c=new Car();
			Store s=new Store();
			c.setId(id);
			c.setCarnum(carnum);
			c.setCarname(carname);
			c.setCarair(carair);
			ww.setServiceExp(serviceExp);
			ww.setServiceDate(day);
			ww.setServiceMoney(serviceMoney);
			
			
			ww.setRemake(remark);
			ww.setCar(c);
			ww.setStore(s);
			w.update(ww);
			
			//request.getRequestDispatcher("Wxshow.jsp").forward(request, response);
			response.sendRedirect("Wx.do?op=show");
		}
		protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			WxService w=new WxServiceImpl();
			int nid = Integer.parseInt(request.getParameter("id"));

			w.delWx(nid);
			response.sendRedirect("Wx.do?op=show");
		}
}
